#include "aky_com_include.h"


uint8 simulate_uart_buff[SIMULATE_UART_BUFF_MAX_SIZE];
uint16 simulate_uart_buff_receive_count = 0; // 一共接收了多少个数据计数
uint16 simulate_uart_buff_read_count = 0;    // 从缓存读取了多少个数据计数
stru_cmd_decode_typedef stru_cmd_decode = {0};
volatile uint16 uart_rx_time_out = 0;
volatile uint8 flag_uart_get_data = 0;
volatile uint8 flag_is_uart_cmd_ture = 0;

void simulate_uart_stru_init(void)
{
    stru_cmd_decode.decode_cmd = E_CMD_IDLE;
}
// 把串口缓冲区的数据一个个提取出来
uint8 simulate_uart_buff_get_1_data(void)
{
    uint8 res = 0;
    res = simulate_uart_buff[simulate_uart_buff_read_count];
    simulate_uart_buff_read_count++; // 已经读出了多少个数据

    if (simulate_uart_buff_read_count >= simulate_uart_buff_receive_count) // 只要把全部数据都读完，马上把缓冲区清零
    {
        simulate_uart_buff_receive_count = 0;
        simulate_uart_buff_read_count = 0;
    }
    return res;
}
void Buf_clear(void) // 把缓冲区下标清零
{
    simulate_uart_buff_receive_count = 0;
    simulate_uart_buff_read_count = 0;
}

// 发送回复指令
void send_ack_cmd(e_cmd_typedef cmd)
{
    uint8 temp_buff[15] = {0};
    uint8 ack_size = 0;

    uint8 i = 0;
    switch (cmd)
    {
    case E_CMD_START_COMMAND: // 开始指令
        ack_size = 4;
        temp_buff[0] = 0xFD;
        temp_buff[1] = 0xF0;
        temp_buff[2] = 0x0D;
        temp_buff[3] = 0x0A;
        break;
    case E_CMD_300_PRES_FOR_CALIBRATION: // 发送标准300点压力指令，用于校正
        ack_size = 10;
        temp_buff[0] = 0xFD;
        temp_buff[1] = 0xF1;
        temp_buff[2] = (stru_cmd_decode.after_cal_pres & 0xff00) >> 8;
        temp_buff[3] = (stru_cmd_decode.after_cal_pres & 0x00ff);
        temp_buff[4] = (stru_cmd_decode.point_300_count & 0xff00) >> 8;
        temp_buff[5] = (stru_cmd_decode.point_300_count & 0x00ff);
        temp_buff[6] = (stru_cmd_decode.point_zero_count & 0xff00) >> 8;
        temp_buff[7] = (stru_cmd_decode.point_zero_count & 0x00ff);
        temp_buff[8] = 0x0D;
        temp_buff[9] = 0x0A;
        break;
    case E_CMD_300_PRES_FOR_COMPARING_LINEARITY: // 发送300点压力指令，用于比对线性
        ack_size = 6;
        temp_buff[0] = 0xFD;
        temp_buff[1] = 0xF2;
        temp_buff[2] = (stru_cmd_decode.after_cal_pres & 0xff00) >> 8;
        temp_buff[3] = (stru_cmd_decode.after_cal_pres & 0x00ff);
        temp_buff[4] = 0x0D;
        temp_buff[5] = 0x0A;
        break;
    case E_CMD_200_PRES_FOR_COMPARING_LINEARITY: // 发送200点压力指令，用于比对线性
        ack_size = 6;
        temp_buff[0] = 0xFD;
        temp_buff[1] = 0xF3;
        temp_buff[2] = (stru_cmd_decode.after_cal_pres & 0xff00) >> 8;
        temp_buff[3] = (stru_cmd_decode.after_cal_pres & 0x00ff);
        temp_buff[4] = 0x0D;
        temp_buff[5] = 0x0A;
        break;
    case E_CMD_100_PRES_FOR_COMPARING_LINEARITY: // 发送100点压力指令，用于比对线性
        ack_size = 6;
        temp_buff[0] = 0xFD;
        temp_buff[1] = 0xF4;
        temp_buff[2] = (stru_cmd_decode.after_cal_pres & 0xff00) >> 8;
        temp_buff[3] = (stru_cmd_decode.after_cal_pres & 0x00ff);
        temp_buff[4] = 0x0D;
        temp_buff[5] = 0x0A;
        break;
    case E_CMD_END_CMD: // 发送结束指令
        ack_size = 4;
        temp_buff[0] = 0xFD;
        temp_buff[1] = 0xF5;
        temp_buff[2] = 0x0D;
        temp_buff[3] = 0x0A;
        break;

    default:
        break;
    }

    if (ack_size == 0)
    {
        return;
    }

    for (i = 0; i < ack_size; i++)
    {
        simulate_uart_send_byte(temp_buff[i]);
    }
}

// 返回的命令类型 如果比对与传参一致 则返回 1
uint8 checke_cmd_ack(e_cmd_typedef cmd)
{
    uint8 res = 0;

    if (stru_cmd_decode.decode_cmd == E_CMD_IDLE)
    {
        return 0;
    }
    if (stru_cmd_decode.decode_cmd == cmd)
    {
        res = 1;
        stru_cmd_decode.decode_cmd = E_CMD_IDLE;
    }

    return res;
}

// 将串口数据缓存解码
// return 1\成功读到一帧数据,并发送ack
uint8 uart_buff_decode_sever(stru_cmd_decode_typedef *p_stru_cmd_decode)
{
    volatile uint8 RCREG_buf_temp[SIMULATE_UART_BUFF_MAX_SIZE] = {0};
//    uint8 Valid_total = 0;
//    uint8 m_int = 0;
//    uint8 Receive_sum = 0;
    uint8 cmd = 0;
    uint8 res = 0;
    uart_rx_time_out++;
    if (uart_rx_time_out < UART_RX_TIME_OUT_MAX)
    {
        return 0;
    }
    if (flag_uart_get_data == 0)
    {
        return 0;
    }
    uart_rx_time_out = 0;
    flag_uart_get_data = 0;

    while (simulate_uart_buff_read_count < simulate_uart_buff_receive_count) // 说明还没有把缓冲区的数据读取完
    {
        RCREG_buf_temp[0] = simulate_uart_buff_get_1_data(); // 数据移动，方便截取关键字
        if (RCREG_buf_temp[0] == 0xFE)                       // 数据头”FE”判断
        {
            // 把余下有效的数据都取出来
            RCREG_buf_temp[1] = simulate_uart_buff_get_1_data(); // 指令类型
            cmd = RCREG_buf_temp[1];
            p_stru_cmd_decode->decode_cmd = cmd;
            p_stru_cmd_decode->last_decode_cmd = cmd;

            RCREG_buf_temp[2] = simulate_uart_buff_get_1_data();        //
            p_stru_cmd_decode->decode_buff[0] = RCREG_buf_temp[2];
            RCREG_buf_temp[3] = simulate_uart_buff_get_1_data();        //
            p_stru_cmd_decode->decode_buff[1] = RCREG_buf_temp[3];
            RCREG_buf_temp[4] = simulate_uart_buff_get_1_data();        //
            RCREG_buf_temp[5] = simulate_uart_buff_get_1_data();        //
            if (RCREG_buf_temp[4] == 0x0D && RCREG_buf_temp[5] == 0x0A) // 检查数据尾
            {
                goto READ_OK;
            }
            else
            {
                goto DECODE_END;
            }

        READ_OK:
            RCREG_buf_temp[0] = 0; // 把临时处理数据清零，方便下次接收
            cmd_decode_pro(p_stru_cmd_decode);
            res = 1;
            break; // 退出循环
        }
    }
DECODE_END:
    Buf_clear(); // 把缓冲区的下标清零,方便下一堆数据接收与处理
    return res;
}

//串口协议解析  放定时器中断处理
void uart_cmd_decode(void)
{
    uint8_t dat = 0;
    if (get_simul_uart_rx_stat() == 1)
    {
        dat = it_simula_uart_get_byte();
        uart_get_buff_server(dat);
    }
    if (uart_buff_decode_sever(&stru_cmd_decode) == 1)
    {
        flag_is_uart_cmd_ture = 1;
    }
}

//return \1 接收到1帧有效协议
uint8_t uart_get_cmd_stat(void)
{
    uint8_t res = 0;
    res = flag_is_uart_cmd_ture;
    flag_is_uart_cmd_ture = 0;
    return res;
}

void cmd_decode_pro(stru_cmd_decode_typedef *p_stru_cmd_decode)
{
    uint16 u16_temp = 0;
//    uint16 u8_temp = 0;
    switch (p_stru_cmd_decode->decode_cmd)
    {
    case E_CMD_START_COMMAND: // 开始指令
        break;
    case E_CMD_300_PRES_FOR_CALIBRATION: // 发送标准300点压力指令，用于校正
        u16_temp = p_stru_cmd_decode->decode_buff[0]<<8 | p_stru_cmd_decode->decode_buff[1];
        p_stru_cmd_decode->receive_cal_pres = u16_temp;
        break;
    case E_CMD_300_PRES_FOR_COMPARING_LINEARITY: // 发送300点压力指令，用于比对线性
        break;
    case E_CMD_200_PRES_FOR_COMPARING_LINEARITY: // 发送200点压力指令，用于比对线性
        break;
    case E_CMD_100_PRES_FOR_COMPARING_LINEARITY: // 发送100点压力指令，用于比对线性
        break;
    case E_CMD_END_CMD: // 发送结束指令
        break;

    default:
        break;
    }
}


// 模拟串口接收数据处理函数 放串口中断处理
void uart_get_buff_server(uint8_t dat)
{
#if (AKY_DEBUG_NO_UATOTEST_ENABLE == 0)
    simulate_uart_buff[simulate_uart_buff_receive_count] = dat;
    // UART_ClearFlagStatus(UART1, UART_Flag_RXNE);
    // simulate_uart_buff[simulate_uart_buff_receive_count] = simulate_uart_read_byte();
    simulate_uart_buff_receive_count++;
    if (simulate_uart_buff_receive_count >= SIMULATE_UART_BUFF_MAX_SIZE)
    {
        simulate_uart_buff_receive_count = SIMULATE_UART_BUFF_MAX_SIZE - 1;
    }
    uart_rx_time_out = 0;
    flag_uart_get_data = 1;

    AKYAutoTestFlag = 1;

#endif
}

void simulate_uart_init(void)
{
    // SIMULATE_UART_RX_PIN_SET_INPUT_PULLUP;
    // SIMULATE_UART_TX_PIN_SET_OUTPUT_IDLE;
    // SIMULATE_UART_RX_PIN_SET_WAKE;
    // SIMULATE_UART_TX_PIN = 1;
    // SIMULATE_UART_RX_PIN = 1;
}

void simulate_uart_delay_us(unsigned int n)
{
    while (n--)
    {
        AKY_ASM_NOP();
        AKY_ASM_NOP();
        AKY_ASM_NOP();
    }
}

void simulate_uart_send_byte(unsigned char dat)
{
    it_simula_uart_send_byte(dat);
}
